package com.wuyou.designPatterns.flyweight;

import java.util.HashMap;
import java.util.Map;

/**
 * 生成BigChar的实例工厂，实现了共享实例的功能
 *
 * @Date 2024/10/18 10:58
 * @Author 无忧
 */
public class BigCharFactory {
    // 管理已经生成的BigChar实例
    private final Map<String, BigChar> pool = new HashMap<>();
    // 单例
    private static final BigCharFactory singleton = new BigCharFactory();

    private BigCharFactory(){}

    public static BigCharFactory getInstance(){
        return singleton;
    }

    public synchronized BigChar getBigChar(char charName){
        BigChar bc = (BigChar)pool.get("" + charName);
        if (bc == null) {
            bc = new BigChar(charName);// 生成 charName 的实例
            pool.put(charName+"", bc);
        }
        return bc;
    }

}
